package com.back_end.NetWork.TCP_DEMO;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

//CLient客户端
public class TcpClientDemo01 {
    public static void main(String[] args) throws UnknownHostException {

        Socket socket = null;
        OutputStream  os = null;

        try {
            //1.要知道服务器地址，端口号
            InetAddress serveIp = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //2.创建一个socket连接
            socket = new Socket(serveIp,port);

            //3.发送消息IO流
            os = socket.getOutputStream();

            os.write("你好".getBytes());

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(os != null){
                try{
                    os.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }

            if(socket != null){
                try {
                    socket.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }


        }
    }
}
